<?php




namespace App\Http\Controllers\Api\User;


use App\Http\Controllers\Api\BasicController;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\Users\UserSocialite;
use App\Services\Email;
use App\Services\Sms;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Mail;
use Jiannei\Response\Laravel\Support\Facades\Response;

class WechatController extends BasicController
{

    /**
     * @api {post} /user/wechat/app 微信APP登陆
     * @apiName UserWechatAPP
     * @apiGroup UserWechat
     * @apiDescription 微信APP登陆
     *
     * @apiParam {String} avatar
     * @apiParam {String} openid
     * @apiParam {String} nickname
     */
    public function app()
    {
        $data = $this->data(['avatar', 'openid', 'nickname']);

        $social = (new UserSocialite())->getSocialite($data['openid'], UserSocialite::WECHAT, $data);
        if ($social->uid > 0) {
            $user = (new User())->getUser(['id'=>$social->uid]);
            $token = (new User())->token($user);
            return Response::success($token);
        } else {
            $insert=[
                'avatar'=>'images/avatar.png',
                'name'=>$data['nickname'],
            ];
            $user = (new User())->createUser($insert);
            $token = (new User())->token($user);
            $res=(new UserSocialite())->updateUid($social->id, $user->id);
            return Response::success($token);
        }
    }


    /**
     * @api {post} /user/wechat/mobile  绑定手机
     * @apiName UserWechatMobile
     * @apiGroup UserWechat
     * @apiDescription 绑定手机
     *
     * @apiHeader {String} Authorization='Bearer '
     *
     * @apiParam {String} id  ID
     * @apiParam {String} mobile  手机号
     * @apiParam {String} code   验证码
     */
    public function mobile()
    {
        $data = $this->data(['id', 'mobile', 'code']);
        $where['mobile'] = $data['mobile'];
        (new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_CHANGE);
        $user = (new User())->getUser($where);
        if (empty($user)) {
            $insert['mobile'] = $data['mobile'];
            $user = (new User())->createUser($insert);
        }
        (new UserSocialite())->updateUid($data['id'], $user->uid);
        $token = (new User())->token($user);
        return Response::success($token);
    }

    public function login(){
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";
        $code = request()->input('code');
        $AppID="wx533d99fa3fed3ba0";
        $AppSecret="fa51bea4ae7cc276cab8ccbcb4f871a5";
        $url=sprintf($url,$AppID,$AppSecret,$code);
        $client=new Client(['timeout'=>5,'verify'=>false]);
        $res=$client->get($url);
        $body=(string)$res->getBody();
        $arr=json_decode($body,true);
        return Response::success();

    }

    public function sendEmail()
    {
        $params = $this->data(['email']);
        (new Email())->send($params['email']);
        return Response::ok("发送成功");
    }

    public function email()
    {
        $data = $this->data(['id','email', 'code']);
        $where['email'] = $data['email'];
        $res=(new Email())->check($data['email'], $data['code'], 1);
        if(!$res) return Response::fail('验证码错误');
        $user = (new User())->getUser($where);
        if (empty($user)) {
            $insert['email'] = $data['email'];
            $insert['avatar'] = 'images/avatar.png';
            $insert['name'] = "用户-".rand(1000,9999);
            $user = (new User())->createUser($insert);
        }
        if(!empty($data['id']))
        (new UserSocialite())->updateUid($data['id'], $user->uid);
        $token = (new User())->token($user);
        return Response::success($token);
    }
}
